package com.chixing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chixing.entity.Coupon;
import com.chixing.entity.Dish;
import com.chixing.mapper.CouponMapper;
import com.chixing.service.ICouponService;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author smith
 * @since 2025-05-21
 */
@Service
public class CouponServiceImpl implements ICouponService {

    @Autowired
    private CouponMapper couponMapper;

    @Override
    public Result getById(Integer couponId) {
        Coupon coupon = couponMapper.selectById(couponId);
        if (coupon != null) {
            return Result.getSuccess(coupon);
        }
        return Result.getFail("优惠券不存在");
    }

    @Override
    public Result getAll() {
        QueryWrapper<Coupon> wrapper = new QueryWrapper<>();
        wrapper.gt("coupon_end_time", LocalDateTime.now()); // 只查询未过期优惠券
        List<Coupon> couponList = couponMapper.selectList(wrapper);
        if (couponList != null && !couponList.isEmpty()) {
            return Result.getSuccess(couponList);
        }
        return Result.getFail("暂无可用优惠券");
    }

    @Override
    public Result getByTypeId(String couponType) {
        QueryWrapper<Coupon> wrapper = new QueryWrapper<>();
        wrapper.eq("coupon_type", couponType);
        List<Coupon> couponList = couponMapper.selectList(wrapper);
        if (couponList != null && !couponList.isEmpty()) {
            return Result.getSuccess(couponList);
        }
        return Result.getFail("当前类型暂无优惠券");
    }

    @Override
    public Result getAllType() {
        List<String> typeList = couponMapper.selectAllCouponTypes();
        if (typeList != null && !typeList.isEmpty()) {
            return Result.getSuccess(typeList);
        }
        return Result.getFail("暂无优惠券类型");
    }

    @Override
    public Result update(Coupon coupon) {
        coupon.setUpdateTime(LocalDateTime.now());
        // update dish set ...... where dish_id = ?
        int rows = couponMapper.updateById(coupon);
        if(rows >0)
            return Result.updateSuccess(coupon);
        return Result.updateFail(coupon);
    }
    /*
    @Override
    public Result mycoupon() {
        QueryWrapper<Coupon> wrapper = new QueryWrapper<>();
        wrapper.eq("other1", 1); // 查询已领取的优惠券

        List<Coupon> myList = couponMapper.selectList(wrapper);
        if (myList != null && !myList.isEmpty()) {
            return Result.getSuccess(myList);
        }
        return Result.getFail("你还没有领取任何优惠券");
    }
     */

}
